package interviewType

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/interviewType"
	interviewTypeReq "github.com/flipped-aurora/gin-vue-admin/server/model/interviewType/request"
)

type InterviewTypeService struct {
}

// CreateInterviewType 创建interviewType表记录
// Author [piexlmax](https://github.com/piexlmax)
func (interviewTypesService *InterviewTypeService) CreateInterviewType(interviewTypes *interviewType.InterviewType) (err error) {
	err = global.GVA_DB.Create(interviewTypes).Error
	return err
}

// DeleteInterviewType 删除interviewType表记录
// Author [piexlmax](https://github.com/piexlmax)
func (interviewTypesService *InterviewTypeService) DeleteInterviewType(interviewTypes interviewType.InterviewType) (err error) {
	err = global.GVA_DB.Delete(&interviewTypes).Error
	return err
}

// DeleteInterviewTypeByIds 批量删除interviewType表记录
// Author [piexlmax](https://github.com/piexlmax)
func (interviewTypesService *InterviewTypeService) DeleteInterviewTypeByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]interviewType.InterviewType{}, "id in ?", ids.Ids).Error
	return err
}

// UpdateInterviewType 更新interviewType表记录
// Author [piexlmax](https://github.com/piexlmax)
func (interviewTypesService *InterviewTypeService) UpdateInterviewType(interviewTypes interviewType.InterviewType) (err error) {
	err = global.GVA_DB.Save(&interviewTypes).Error
	return err
}

// GetInterviewType 根据id获取interviewType表记录
// Author [piexlmax](https://github.com/piexlmax)
func (interviewTypesService *InterviewTypeService) GetInterviewType(id uint) (interviewTypes interviewType.InterviewType, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&interviewTypes).Error
	return
}

// GetInterviewTypeInfoList 分页获取interviewType表记录
// Author [piexlmax](https://github.com/piexlmax)
func (interviewTypesService *InterviewTypeService) GetInterviewTypeInfoList(info interviewTypeReq.InterviewTypeSearch) (list []interviewType.InterviewType, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&interviewType.InterviewType{})
	var interviewTypess []interviewType.InterviewType
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	if info.Level != nil {
		db = db.Where("level = ?", info.Level)
	}
	if info.Name != "" {
		db = db.Where("name = ?", info.Name)
	}
	if info.Pid != nil {
		db = db.Where("pid = ?", info.Pid)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&interviewTypess).Error

	// 汇总父级id 放在一个切片
	tyids := []int{}
	for _, val := range interviewTypess {
		tyids = append(tyids, int(val.ID))
	}

	//定于父级结构体
	var interviewPidTypess []interviewType.InterviewType

	// 使用tyids切片查询所有父id数据并放在interviewPidTypess
	err = db.Where("id in(?)", tyids).Find(&interviewPidTypess).Error
	if err != nil {
		return nil, 0, err
	}

	// 格式化interviewPidTypess 数据到map key为id,值就是整行数据
	interviewPidTypessMap := map[uint]interviewType.InterviewType{}
	for _, val := range interviewPidTypess {
		interviewPidTypessMap[val.ID] = val
	}

	// 遍历interviewTypess 给pidName字段赋值
	//for key, val := range interviewTypess {
	//	//检查这个父id是否存在，不存在就跳过，默认空值
	//	name := "根节点"
	//	pidInfo, ok := interviewPidTypessMap[uint(*val.Pid)]
	//	//pidInfo, ok := interviewPidTypessMap[val.Pid]
	//	if ok {
	//		name = pidInfo.Name
	//	}
	//
	//	interviewTypess[key].PidName = name
	//}
	return interviewTypess, total, err
}
